package mygw.jtthink.com.Controllers;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import mygw.jtthink.com.Utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class AccessToken {

    @Autowired
    private TokenUtil tokenUtil;

    @GetMapping(path = "/access_token")
    public Map<String,Object> token(@RequestParam("uname") String uname
            ,@RequestParam("upass") String upass
    ){
        Map<String,Object> result=new HashMap<>();
        result.put("token","");
        if(uname.equals("shenyi") && upass.equals("123") || (uname.equals("lisi") && upass.equals("123"))){
            Object ret=tokenUtil.checkToken(uname);
            if(ret!=null){
                return (Map)ret;
            }

        }else {
            return result;
        }

        String token=tokenUtil.genToken(uname);//这一步生成了token
        result.put("token",token);
        result.put("expire", tokenUtil.setToken(uname,token)); //单位：秒

        return  result;
    }
}
